package code.Array;

import java.util.Arrays;

public class maximumProduct {
    public static void main(String[] args) {
        int[] array = {-5, -6, -3,1,2,3};
        System.out.println("Output:" + maximumProduct(array));
    }

    public static int maximumProduct(int[] nums) {
        System.out.println("Input:" + Arrays.toString(nums));
        //-10 -20  -5 2
        //有三个正整数 直接拿到最大的三个数
        //全为负整数 直接拿最大的三个数
        //仅有两个正整数 一个负数  拿两个负数和一个正整数

        //总结：只有两种情况  两个最小的负数相乘 * 最大的整数
        //                 三个最大的整数相乘
        int firstMax = Integer.MIN_VALUE;
        int secondMax = Integer.MIN_VALUE;
        int thirdMax = Integer.MIN_VALUE;

        int firstMin = Integer.MAX_VALUE;
        int secondMin = Integer.MAX_VALUE;
        for (int num : nums) {
            //获取最大的三个数
            if (num > firstMax) {
                thirdMax = secondMax;
                secondMax = firstMax;
                firstMax = num;
            } else if (num > secondMax) {
                thirdMax = secondMax;
                secondMax = num;
            } else if (num >= thirdMax) {
                thirdMax = num;
            }
        }
        for (int num : nums) {
            //获取最小的两个数
            //if (num == firstMin || num == secondMin) continue;
            if (num < firstMin) {
                secondMin = firstMin;
                firstMin = num;
            } else if (num < secondMin) {
                secondMin = num;
            }
        }
        return Math.max(firstMax * secondMax * thirdMax, firstMax * firstMin * secondMin);
    }
}
